package com.sskj.postar.controller;

import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.vod.model.v20170321.GetPlayInfoResponse;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.sskj.postar.core.ret.HttpResponse;
import com.sskj.postar.core.ret.HttpResult;
import com.sskj.postar.core.utils.*;
import com.sskj.postar.model.Information;
import com.sskj.postar.model.Mail;
import com.sskj.postar.model.User;
import com.sskj.postar.service.InformationService;
import com.sskj.postar.service.MailService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.File;
import java.io.IOException;
import java.util.List;

@Api(tags = "公共接口")
@RestController
@RequestMapping("/common")
public class CommonController {
    @Resource
    private MailService mailService;
    @Resource
    private InformationService informationService;
    @Resource
    private RedisUtil redisUtil;
    @Value("${config.code.timeout}")
    long timeout;
    @Value("${config.img.filepath}")
    String filepath;

    /**
     * 测试Ｓｗａｇｇｅｒ
     *
     * @param sendTo
     * @return 验证码
     * @throws Exception
     */
    @ApiOperation(value = "测试Ｓｗａｇｇｅｒ", notes = "123")
    @ApiImplicitParams({
            @ApiImplicitParam(value = "发送目标", name = "sendTo", paramType = "query")
    })
    @PostMapping("/testSwagger")
    public HttpResult<User> testSwagger(String sendTo) throws Exception {
        return HttpResponse.makeOKRsp();
    }

    /**
     * 发送注册验证码
     *
     * @param sendTo
     * @return 验证码
     * @throws Exception
     */
    @ApiOperation(value = "发送注册验证码")
    @ApiImplicitParams({
            @ApiImplicitParam(value = "发送目标", name = "sendTo", paramType = "query")
    })
    @PostMapping("/sendCode")
    public HttpResult<String> sendCode(String sendTo) throws Exception {
        String identifyingCode = ApplicationUtils.getNumStringRandom(6);
        if (sendTo.contains("@")) {
            if (CheckUtil.isEmail(sendTo)) {
                Mail mail = MailUtil.createMail(sendTo, identifyingCode);
                mailService.sendTemplateMail(mail);
                redisUtil.set(sendTo, identifyingCode, timeout);
                return HttpResponse.makeOKRsp(identifyingCode);
            } else {
                return HttpResponse.makeErrRsp("邮箱格式错误");
            }
        } else if (CheckUtil.isMobile(sendTo)) {
            redisUtil.set(sendTo, identifyingCode, timeout);

            return HttpResponse.makeOKRsp(identifyingCode);
        } else {
            return HttpResponse.makeErrRsp("手机格式错误");
        }
    }

    /**
     * 验证验证码
     *
     * @param sendTo 手机号码或者邮箱
     * @param code   接收的验证码
     * @return
     */
    @ApiOperation(value = "验证验证码")
    @ApiImplicitParams({
            @ApiImplicitParam(value = "手机号码或者邮箱", name = "sendTo", paramType = "query"),
            @ApiImplicitParam(value = "接收的验证码", name = "code", paramType = "query")
    })
    @PostMapping("/checkCode")
    public HttpResult checkCode(@RequestParam String sendTo, @RequestParam String code) {
        String saveCode = (String) redisUtil.get(sendTo);
        if (saveCode == null) {
            return HttpResponse.makeErrRsp("验证码已过期");
        }
        if (!code.equals(saveCode)) {
            return HttpResponse.makeErrRsp("验证码错误");
        }
        return HttpResponse.makeOKRsp("验证通过", null);
    }

    /**
     * 上传图片
     *
     * @param img 图片
     * @return
     * @throws IOException
     */
    @ApiOperation(value = "上传图片")
    @ApiImplicitParams({
            @ApiImplicitParam(value = "图片", name = "img", paramType = "query"),
    })
    @PostMapping("/uploadImg")
    public HttpResult uploadImg(MultipartFile img) throws IOException {
        img.transferTo(new File(filepath + img.getOriginalFilename()));
        return HttpResponse.makeOKRsp("图片上传成功", img.getOriginalFilename());
    }

    /**
     * 支付宝签名
     *
     * @param price 价格
     * @return
     */
    @ApiOperation(value = "支付宝签名")
    @ApiImplicitParams({
            @ApiImplicitParam(value = "价格", name = "price", paramType = "query"),
    })
    @GetMapping("/alipay/sign")
    public HttpResult sign(String price) {
        String sign = AlipayUtil.sign();
        return HttpResponse.makeOKRsp("成功", sign);
    }

    /**
     * 获取视频信息
     *
     * @param videoId 视频ID
     * @return
     * @throws Exception
     */
    @ApiOperation(value = "获取视频信息")
    @ApiImplicitParams({
            @ApiImplicitParam(value = "视频ID", name = "videoId", paramType = "query"),
    })
    @GetMapping("/videoInfo")
    public HttpResult<GetPlayInfoResponse> getVideoInfo(String videoId) throws Exception {
        DefaultAcsClient defaultAcsClient = AliVideoUtil.initVodClient("LTAIyQmdzCjTviRM", "xbuJ29Fwj2P4m0msuZVE7fq5E63MQU");
        GetPlayInfoResponse playInfo = AliVideoUtil.getPlayInfo(defaultAcsClient, videoId);
        return HttpResponse.makeOKRsp(playInfo);
    }
    @GetMapping("/information")
    public HttpResult<PageInfo<Information>> getInformation(int page,int pageSize){
            PageHelper.startPage(page, pageSize);
            List<Information> list = informationService.getAll();
            PageInfo<Information> pageInfo = new PageInfo<Information>(list);
            return HttpResponse.makeOKRsp(pageInfo);
    }
}
